package com.imohoo.testre;

import com.google.gson.Gson;
import com.imohoo.git.GitHubService;
import com.imohoo.git.Repo;
import com.imohoo.libs.http.OkHttpUtils;
import com.imohoo.libs.retrofit2.SpRetrofitManager;
import com.imohoo.libs.retrofit2.converter.SpFile;
import com.imohoo.libs.retrofit2.converter.SpJsonConverterFactory;
import com.imohoo.libs.retrofit2.converter.SpRequest;
import com.imohoo.libs.retrofit2.converter.SpResponse;
import okhttp3.OkHttpClient;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * Created by zhaobo on 2017/1/6.
 */
public class Test {
    public static void main(String[] args) {
//        listgit();
//        listZan1();
//        download();

        FileUploadRequest fileRequest = new FileUploadRequest();
        fileRequest.setPrefix(FileUploadRequest.prefix_video);
        fileRequest.setUser_id(20);
        fileRequest.setToken("b372ead0e4ce156e9640b7a56568d68c");
//        fileRequest.setToken("bb0123761e5aef27951600db2e9261bb");
        fileRequest.setVideo_time(1);
        fileRequest.setThumb_id(110);
        fileRequest.setFileMD5(MD5Utils.getFileMD5("e:\\a.mp4"));
        fileRequest.setSingleFile();
        uploadFile2(fileRequest, "e:\\a.mp4");
    }

    private static void uploadFile2(FileUploadRequest request, String filePath) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://video.myrunners.com/")
                .addConverterFactory(SpJsonConverterFactory.create())
                .client(new OkHttpClient.Builder()
                        .connectTimeout(60, TimeUnit.SECONDS)
                        .writeTimeout(60, TimeUnit.SECONDS)
                        .readTimeout(60, TimeUnit.SECONDS)
                        .build())
                .build();

        ShanpaoService service = retrofit.create(ShanpaoService.class);
        Call<FileUploadResponse> call = service.uploadFile2(new SpFile(request, request.getFiles(), filePath));
        call.enqueue(new Callback<FileUploadResponse>() {
            @Override
            public void onResponse(Call<FileUploadResponse> call, Response<FileUploadResponse> response) {
                System.out.println(response.message());
                System.out.println(new Gson().toJson(response.body()));
                try {
                    System.out.println(response.errorBody().string());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<FileUploadResponse> call, Throwable t) {
                t.printStackTrace();
            }
        });
    }

    private static void listgit() {
        try {
            GitHubService service = SpRetrofitManager.getRetrofit().create(GitHubService.class);

            Call<List<Repo>> call = service.listRepos("axter");
            call.enqueue(new Callback<List<Repo>>() {
                @Override
                public void onResponse(Call<List<Repo>> call, Response<List<Repo>> response) {
                    System.out.println(new Gson().toJson(response.body()));
                }

                @Override
                public void onFailure(Call<List<Repo>> call, Throwable t) {
                    t.printStackTrace();
                }
            });
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private static void listZan1() {
        try {
            ShanpaoService service = SpRetrofitManager.getRetrofit().create(ShanpaoService.class);
            Call<SpResponse<HashMap>> call = service.listZan2(new SpRequest("SnsApi", "getUserHitsList").set("action", 1).set("page", 0));
            call.enqueue(new Callback<SpResponse<HashMap>>() {
                @Override
                public void onResponse(Call<SpResponse<HashMap>> call, Response<SpResponse<HashMap>> response) {
                    System.out.println(new Gson().toJson(response.body()));
                }

                @Override
                public void onFailure(Call<SpResponse<HashMap>> call, Throwable t) {
                    t.printStackTrace();
                }
            });
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private static void listZan2() {
        try {

            ShanpaoService service = SpRetrofitManager.getRetrofit().create(ShanpaoService.class);

            Call<SpResponse<HashMap>> call = service.listZan1(new ZanRequest().setAction(1).setPage(0));
//            Call<SpResponse<HashMap>> call = service.listZan2(new SpRequest().add("action", 1).add("page", 0));
            call.enqueue(new Callback<SpResponse<HashMap>>() {
                @Override
                public void onResponse(Call<SpResponse<HashMap>> call, Response<SpResponse<HashMap>> response) {
                    System.out.println(new Gson().toJson(response.body()));
                }

                @Override
                public void onFailure(Call<SpResponse<HashMap>> call, Throwable t) {
                    t.printStackTrace();
                }
            });
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private static void download() {
        String url = "https://dev-file.myrunners.com/Upload/default/2016/10/25/484cf2b624e7446d9374775ea8297d0e.jpg";
        ShanpaoService service = SpRetrofitManager.getRetrofit().create(ShanpaoService.class);
        Call<ResponseBody> call = service.download(url);
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                writeResponseBodyToDisk(response.body());
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable throwable) {

            }
        });
    }

    private static boolean writeResponseBodyToDisk(ResponseBody body) {
        try {
            // todo change the file location/name according to your needs
            File futureStudioIconFile = new File("e:\\a.jpg");

            InputStream inputStream = null;
            OutputStream outputStream = null;

            try {
                byte[] fileReader = new byte[4096];

                long fileSize = body.contentLength();
                long fileSizeDownloaded = 0;

                inputStream = body.byteStream();
                outputStream = new FileOutputStream(futureStudioIconFile);

                while (true) {
                    int read = inputStream.read(fileReader);

                    if (read == -1) {
                        break;
                    }

                    outputStream.write(fileReader, 0, read);

                    fileSizeDownloaded += read;

                    System.out.println("file download: " + fileSizeDownloaded + " of " + fileSize);
                }

                outputStream.flush();

                return true;
            } catch (IOException e) {
                return false;
            } finally {
                if (inputStream != null) {
                    inputStream.close();
                }

                if (outputStream != null) {
                    outputStream.close();
                }
            }
        } catch (IOException e) {
            return false;
        }
    }
}
